package org.example.testdemo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.example.testdemo.pojo.Project;
import org.example.testdemo.pojo.combination.ProjectList;
import org.example.testdemo.pojo.combination.ProjectPid;
import org.example.testdemo.pojo.combination.ProjectUpdate;
import org.example.testdemo.pojo.combination.UserInformation;

import java.util.List;

@Mapper
public interface ProjectMapper extends BaseMapper<Project> {

    @Select("<script>" +
            "select p_id as value from project" +
            "<if test='pName != null'>" +
            " where p_id LIKE CONCAT('%', #{pName}, '%')" +
            "</if>" +
            "</script>")
    public List<ProjectPid> getProjectPname(@Param("pName") String pName);

    @Select("<script>" +
            "select p_id as value from project where p_id=#{pId}" +
            "</script>")
    public List<ProjectPid> querySubordinateProjects(@Param("pId")String pId);

    @Select("<script>" +
            "select p_id as value from project where p_id LIKE CONCAT(#{pId}, '%')" +
            "</script>")
    public List<ProjectPid> subordinateProjects(@Param("pId")String pId);

    @Update("update project set details=0 where p_id=#{pId}")
    public Integer updateDetails(@Param("pId")String pId);

    @Select("<script>" +
            "SELECT a.`p_id`,a.`p_name`,a.`level`,a.`details`,a.`state`,a.`balanceDirection`,b.`name` as uName,c.`b_Name` FROM `project` AS a,`user` AS b,`department` AS c WHERE a.`b_Code`=c.`b_Code` AND a.`userId`=b.`userId` " +
            "<if test='pName != null'>" +
            " and a.p_name LIKE CONCAT('%', #{pName}, '%') " +
            "</if>" +
            "<if test='pId != null'>" +
            " and a.p_id LIKE CONCAT('%', #{pId}, '%') " +
            "</if>" +
            "<if test='bCode != null'>" +
            " and a.b_Code = #{bCode}" +
            "</if>" +
            "<if test='name != null'>" +
            " and b.name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "</script>")
    public IPage<ProjectList> getProjectList(String pName,String bCode,String name,String pId,IPage<?> page);

    @Select("<script>" +
            "SELECT a.`p_id`,a.limitPayment,a.`p_name`,a.`state`,a.`balanceDirection`,b.`name` as uName,c.`b_Name`,b.userId,c.b_Code FROM `project` AS a,`user` AS b,`department` AS c WHERE a.`b_Code`=c.`b_Code` AND a.`userId`=b.`userId` " +
            " and a.`p_id`=#{pId}" +
            "</script>")
    public ProjectUpdate obtainProjectInformation(@Param("pId")String pId);

    @Update("update project set state = #{state} where p_id LIKE CONCAT(#{pId}, '%')")
    public void updateProjectAll(String pId,Integer state);

    @Delete("<script>" +
            "delete from project where " +
            "<foreach item='pId' collection='pIds' open='' separator='or' close=''>" +
            "p_id LIKE CONCAT(#{pId}, '%') " +
            "</foreach>" +
            "p_id = -1" +
            "</script>")
    public Integer deleteProject(String[] pIds);

    @Select("<script>" +
            "SELECT a.`p_id`,a.`p_name`,a.`level`,a.`details`,a.`state`,a.`balanceDirection`,b.`name` as uName,c.`b_Name` FROM `project` AS a,`user` AS b,`department` AS c WHERE a.`b_Code`=c.`b_Code` AND a.`userId`=b.`userId` " +
            "<if test='pName != null'>" +
            " and a.p_name LIKE CONCAT('%', #{pName}, '%') " +
            "</if>" +
            "<if test='pId != null'>" +
            " and a.p_id LIKE CONCAT('%', #{pId}, '%') " +
            "</if>" +
            " and a.userId = #{userId}" +
            "</script>")
    public IPage<ProjectList> getMyProjects(String pName,String pId,String userId,IPage<?> page);

    @Select("select p_id,p_name,b_Code as b_Name from project where userId=#{userId}")
    public List<ProjectList> getAutProjectList(@Param("userId") String userId);

    @Select("<script>" +
            "SELECT a.`p_id`,a.`p_name`,a.`level`,a.`details`,a.`state`,a.`balanceDirection`,b.`name` as uName,c.`b_Name` FROM `project` as a,user as b,`department` AS c WHERE a.`b_Code`=c.`b_Code` AND a.userId=b.userId and `p_id` IN ((SELECT `p_id` FROM `authorize` WHERE `userId`=#{userId})) AND state=1 and balanceDirection=0 " +
            "<if test='pName != null'>" +
            " and a.p_name LIKE CONCAT('%', #{pName}, '%') " +
            "</if>" +
            "<if test='pId != null'>" +
            " and a.p_id LIKE CONCAT('%', #{pId}, '%') " +
            "</if>" +
            "</script>")
    public IPage<ProjectList> authorizeProject(String userId, String pName, String pId, Page<ProjectList> page);
}
